package chen.web.forum;

import chen.web.event.EventSupport;
import chen.web.forum.event.PostCheckedEvent;
import chen.web.forum.event.PostDeletedEvent;
import chen.web.forum.event.PostMovedEvent;
import chen.web.forum.event.PostPublishedEvent;
import chen.web.forum.event.PostRestoredEvent;

public class TopicManagerEventFirer extends BaseTopicManagerProxy implements
		TopicManager {

	private EventSupport es;
	
	public TopicManagerEventFirer(TopicManager tm, EventSupport es) {
		super(tm);
		this.es = es;
		// TODO Auto-generated constructor stub
	}

	@Override
	public Subject restore(Subject subject) {
		// TODO Auto-generated method stub
		Subject result = super.restore(subject);
		PostRestoredEvent event = new PostRestoredEvent(result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public Reply restore(Reply reply) {
		// TODO Auto-generated method stub
		Reply result = super.restore(reply);
		PostRestoredEvent event = new PostRestoredEvent(result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public void delete(Subject subject) {
		// TODO Auto-generated method stub
		super.delete(subject);
		PostDeletedEvent event = new PostDeletedEvent(subject, false);
		es.fireEvent(event);
	}

	@Override
	public void delete(Reply reply) {
		// TODO Auto-generated method stub
		super.delete(reply);
		PostDeletedEvent event = new PostDeletedEvent(reply, false);
		es.fireEvent(event);
	}

	@Override
	public Subject post(Subject subject) throws NotLowestException {
		// TODO Auto-generated method stub
		Subject result = super.post(subject);
		PostPublishedEvent event = new PostPublishedEvent(result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public Reply post(Reply reply) {
		// TODO Auto-generated method stub
		Reply result =  super.post(reply);
		PostPublishedEvent event = new PostPublishedEvent(result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public Subject move(Subject subject, Category category)
			throws NotLowestException {
		// TODO Auto-generated method stub
		Category source = subject.getCategory();
		Subject result = super.move(subject, category);
		PostMovedEvent event = new PostMovedEvent(result, source);
		es.fireEvent(event);
		return result;
	}

	@Override
	public boolean safeDelete(Subject subject) {
		// TODO Auto-generated method stub
		boolean result = super.safeDelete(subject);
		PostDeletedEvent event = new PostDeletedEvent(subject, !result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public boolean safeDelete(Reply reply) {
		// TODO Auto-generated method stub
		boolean result = super.safeDelete(reply);
		PostDeletedEvent event = new PostDeletedEvent(reply, !result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public Reply checked(Reply reply) {
		// TODO Auto-generated method stub
		Reply result = super.checked(reply);
		PostCheckedEvent event = new PostCheckedEvent(result);
		es.fireEvent(event);
		return result;
	}

	@Override
	public Subject checked(Subject subject) {
		// TODO Auto-generated method stub
		Subject result = super.checked(subject);
		PostCheckedEvent event = new PostCheckedEvent(result);
		es.fireEvent(event);
		return result;
	}


	

}
